package com.lubase.core.controller.handler;

import com.lubase.orm.model.LoginUser;
import com.lubase.orm.service.AppHolderService;
import com.lubase.core.config.PassToken;
import com.lubase.core.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.websocket.AuthenticationException;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 认证拦截器，对认证信息进行校验
 *
 * @author A
 */
@Slf4j
public class AuthenticationInterceptorInterceptor implements HandlerInterceptor {

    AppHolderService appHolderService;

    UserInfoService userService;

    public AuthenticationInterceptorInterceptor(UserInfoService userService, AppHolderService appHolderService) {
        this.appHolderService = appHolderService;
        this.userService = userService;
    }


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod method = (HandlerMethod) handler;

        if (method.getMethod().isAnnotationPresent(PassToken.class) || method.getClass().isAnnotationPresent(PassToken.class)) {
            return true;
        }
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            token = request.getParameter("token");
            if (StringUtils.isEmpty(token)) {
                String url = request.getRequestURL().toString();
                String remoteAddr = request.getRemoteAddr();
                log.info("请求源: {}",remoteAddr);
                log.info("目标地址: {}",url);
                log.info("请求中没有携带token，请检查");
                return false;
            }
        }
        LoginUser user = userService.verifyToken(token);
        if (user == null) {
            throw new AuthenticationException("用户信息已经失效");
        }
        String ssoToken = request.getHeader("ssoToken");
        if (!StringUtils.isEmpty(ssoToken)) {
            ssoToken = request.getParameter("ssoToken");
            if (!StringUtils.isEmpty(ssoToken)) {
                user.setSsoToken(ssoToken);
            }
        }
        String appId = request.getHeader("appId");
        if (!StringUtils.isEmpty(appId)) {
            user.setSessionAppId(Long.parseLong(appId));
        }
        appHolderService.setUser(user);
        return true;
    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        appHolderService.clear();
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
